<?php
/**
 * 自动加载类
 * @param String $className
 */
function __autoload($className) {
    $sysClassPath = sysClassPath();
    if (array_key_exists($className, $sysClassPath)) {
        return require_once($sysClassPath[$className]);
    } else if ('Action' == substr($className, -6)) {
        if (file_exists(ACTION_DIR . '/' . $className . '.class.php')) 
            return require_once(ACTION_DIR . '/' . $className . '.class.php');
    } else if (file_exists(CLASS_PATH . '/' . $className . '.class.php')) {
        return require_once(CLASS_PATH . '/' . $className . '.class.php');
    } else {
        throw new Exception('找不到类"' . $className . '"的定义');
    }
}

/**
 * 系统类文件路径列表
 * @return array
 */
function sysClassPath() {
    static $classPath = array();
    if (0 == sizeof($classPath)) {
        $classPath = include(PUMP_DIR . '/SysFiles/SysClass.php');
    }
    return $classPath;
}

/**
 * 系统表配置文件路径列表
 * @return array
 */
function sysTablePath() {
    static $tablePath = array();
    if (0 == sizeof($tablePath)) {
        $tablePath = include(PUMP_DIR . '/SysFiles/SysTable.php');
    }
    return $tablePath;
}

/**
 * 数据库字符串加引号
 * @param string $str
 */
function quote($str) {
    $appKey = Application::getKey();
    if(NULL === $str || '' === $str) return "''";
    return PUMP_QUOTE_STRING . $str . PUMP_QUOTE_STRING;
}

function UUID() {
    if(function_exists('com_create_guid')) {
        return trim(com_create_guid(), '{}');
    }if(function_exists('uuid_create')) {
        return strtoupper(uuid_create());
    } else {
        $chars = md5(uniqid(mt_rand(), true));  
        $uuid  = substr($chars,0,8) . '-';  
        $uuid .= substr($chars,8,4) . '-';  
        $uuid .= substr($chars,12,4) . '-';  
        $uuid .= substr($chars,16,4) . '-';  
        $uuid .= substr($chars,20,12);  
        return strtoupper($uuid);  
    }
}

function isUUID($uuid) {
    return preg_match('/\w{8}\-\w{4}\-\w{4}\-\w{4}\-\w{12}/i', $uuid);
}

function URL($actionName='', $moduleName='', $args='') {
    static $urls = array();
    $urlKey = md5(serialize($actionName) . '^' . serialize($moduleName) . '^' . serialize($args));
    if (false == isset($urls[$urlKey])) {
        if (is_array($actionName)) {
            $args = array();
            $action = $actionName['action'];
            $module = $actionName['module'];
            $exceptKeys = array('funcName', 'module', 'action', 'var');
            foreach($actionName as $key=>$val) {
                if (false == in_array($key, $exceptKeys)) {
                    $args[$key] = $val;
                }
            }
        } else {
            $module = $moduleName;
            $action = $actionName;
        }
        $urls[$urlKey] = Url::getUrl($action, $module, $args);
    }
    
    return $urls[$urlKey];
}

if(!function_exists('checkdnsrr')) {
    function checkdnsrr($hostName, $recType = '') { 
     if(!empty($hostName)) { 
       if( $recType == '' ) $recType = "MX"; 
       exec("nslookup -type=$recType $hostName", $result); 
       foreach ($result as $line) { 
         if(eregi("^$hostName",$line)) { 
           return true; 
         } 
       } 
       return false; 
     } 
     return false;
    }
}

function dump($var) {
    echo '<pre>';
    print_r($var);
    echo '</pre>';
}

function sbc2dbc($str) {
    $sbcArr = array('０','１','２','３','４','５','６','７','８','９','Ａ','Ｂ','Ｃ','Ｄ','Ｅ','Ｆ','Ｇ','Ｈ','Ｉ','Ｊ','Ｋ','Ｌ','Ｍ','Ｎ','Ｏ','Ｐ','Ｑ','Ｒ','Ｓ','Ｔ','Ｕ','Ｖ','Ｗ','Ｘ','Ｙ','Ｚ','ａ','ｂ','ｃ','ｄ','ｅ','ｆ','ｇ','ｈ','ｉ','ｊ','ｋ','ｌ','ｍ','ｎ','ｏ','ｐ','ｑ','ｒ','ｓ','ｔ','ｕ','ｖ','ｗ','ｘ','ｙ','ｚ','＋','－','＝','？','＜','＞', '．', '＠', '＿');
    $dbcArr = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','+','-','=','?','<','>', '.', '@', '_');
    $replaceArray = array_combine($sbcArr, $dbcArr);
    return strtr($str, $replaceArray);
}

/**
 * 获取用户ip
 */
function getUserIp() {
    if ($_SERVER["HTTP_X_FORWARDED_FOR"]) { 
        $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; 
    } elseif ($_SERVER["HTTP_CLIENT_IP"]) { 
        $ip = $_SERVER["HTTP_CLIENT_IP"]; 
    } elseif ($_SERVER["REMOTE_ADDR"]) { 
        $ip = $_SERVER["REMOTE_ADDR"]; 
    } elseif (getenv("HTTP_X_FORWARDED_FOR")) { 
        $ip = getenv("HTTP_X_FORWARDED_FOR"); 
    } elseif (getenv("HTTP_CLIENT_IP")) { 
        $ip = getenv("HTTP_CLIENT_IP"); 
    } elseif (getenv("REMOTE_ADDR")) { 
        $ip = getenv("REMOTE_ADDR"); 
    } else { 
        $ip = "Unknown"; 
    }
    return $ip;
}

function encodeUrl($url) {
    return base64_encode($url);
}

function decodeUrl($str) {
    return base64_decode($str);
}
?>